<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Image extends MX_Controller {

    private $data = array(
        'dir' => array(
            'original' => 'assets/uploads/original/',
            'thumb' => 'assets/uploads/thumbs/',
            'medium' => 'assets/uploads/medium/'
        ),
        'total' => 0,
        'images' => array(),
        'error' => ''
    );

//	public function __construct()
//    {
//        parent::Controller();
//    }

	public function index($start = 0)
	{
        if ($this->input->post('btn_upload')) {
            $this->upload();
        }

        $this->load->library('pagination');

        $c_paginate['base_url'] = site_url('welcome/proyectos/4/');
        $c_paginate['per_page'] = '16';
        $finish = $start + $c_paginate['per_page'];
        
        if (is_dir($this->data['dir']['thumb']))
        {
            $i = 0;
            if ($dh = opendir($this->data['dir']['thumb'])) {
                while (($file = readdir($dh)) !== false) {
                    // get file extension
                    $ext = strrev(strstr(strrev($file), ".", TRUE));
                    if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png') {
                        if ($start <= $this->data['total'] && $this->data['total'] < $finish) {
                            $this->data['images'][$i]['thumb'] = $file;
                            $this->data['images'][$i]['original'] = str_replace('thumb_', '', $file);
                            $this->data['images'][$i]['medium'] = str_replace('medium_', '', $file);
                            $i++;
                        }
                        $this->data['total']++;
                    }
                }
                closedir($dh);
            }
        }

        $c_paginate['total_rows'] = $this->data['total'];

        $this->pagination->initialize($c_paginate);

		$this->load->view('images/index', $this->data);
	}

    private function upload()
    {
        $c_upload['upload_path']    = $this->data['dir']['original'];
        $c_upload['allowed_types']  = 'gif|jpg|png|jpeg|x-png';
        $c_upload['max_size']       = '3500';
        $c_upload['max_width']      = '3600';
        $c_upload['max_height']     = '3200';
        $c_upload['remove_spaces']  = TRUE;
        $c_upload['encrypt_name'] = TRUE;

        $this->load->library('upload', $c_upload);
        
        if ($this->upload->do_upload()) {
            
            $img = $this->upload->data();
            
            
          $new = array(
               $this->data['dir']['thumb'].'thumb_'.$img['file_name'],
                           
              $this->data['dir']['medium'].'medium_'.$img['file_name'],
             
              );
            
           
          
          
          
          for ($i = 0; $i < 2; $i++) {
    

            $config['image_library'] = 'gd2';
            $config['source_image'] =  $img['full_path']; 
            //$config['create_thumb'] = TRUE;
            //$config['maintain_ratio'] = TRUE;
            $config['width'] = 405;
           $config['height'] = 550;
           $config['new_image']= $new[$i];
           

           $this->load->library('image_lib', $config);
           $this->image_lib->resize();
        }    
            
            
            
            
             
        } else {
            $this->data['error'] = $this->upload->display_errors();
        }
    }

    public function delete($ori_img)
    {
        unlink($this->data['dir']['original'].$ori_img);
        unlink($this->data['dir']['thumb'].'thumb_'.$ori_img);
        redirect('/');
    }
}

/* End of file image.php */
/* Location: ./system/application/controllers/image.php */